﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Sites_Man_Main : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblDate.Text = DateTime.Today.ToString("ddd, dd MMM, yyyy").ToUpper();
        }
    }

    public override void DataBind()
    {
        if (Request.IsAuthenticated)
        {
            var acmsPage = Page as PageBase;
            if (acmsPage != null && acmsPage.User.Identity.User != null)
            {
                //get latest audit
                var auditFactory = new CarbonExchange.Bll.AuditFactory();
                var latestAudit = auditFactory.GetLatestCompletedByUserId(acmsPage.User.Identity.User.Id);
                if (latestAudit != null)
                {
                    //get sections for that audit
                    var sectionFactory = new CarbonExchange.Bll.UserSectionFactory();
                    var sections = sectionFactory.GetAllByAuditId(latestAudit.Id).ToList();
                    if (sections != null)
                    {
                        decimal totalFootprint = sections.Sum(x => x.Footprint);
                        lblTotalFootprint.Text = string.Format("{0:#,##0}", totalFootprint);
                        lblTotalCost.Text = acmsPage.ConvertToCurrencyString(totalFootprint);// string.Format(latestAudit.Country.CultureInfo, "{0:c}", (totalFootprint * latestAudit.Country.Conversion * 0.2m));
                        DataBindSection(sections.Where(x => x.SectionName.Equals("Energy", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(), lblEnergyFootprint, lblEnergyCost, latestAudit);
                        DataBindSection(sections.Where(x => x.SectionName.Equals("Waste", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(), lblWasteFootprint, lblWasteCost, latestAudit);
                        DataBindSection(sections.Where(x => x.SectionName.Equals("Water", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(), lblWaterFootprint, lblWaterCost, latestAudit);
                        DataBindSection(sections.Where(x => x.SectionName.Equals("Transport", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(), lblTransportFootprint, lblTransportCost, latestAudit);
                        DataBindSection(sections.Where(x => x.SectionName.Equals("Shopping", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(), lblShoppingFootprint, lblShoppingCost, latestAudit);
                    }

                }
            }
        }
        base.DataBind();
    }

    private void DataBindSection(CarbonExchange.Bll.UserSection userSection, Label footprintLabel, Label costLabel, CarbonExchange.Bll.Audit audit)
    {
        PageBase page = (PageBase)this.Page;
        if (userSection != null && userSection.Completed)
        {
            footprintLabel.Text = userSection.Footprint.ToString("#,##0");
            costLabel.Text = page.ConvertToCurrencyString(userSection.Footprint);
        }
        else
        {
            footprintLabel.Text = 0m.ToString("#,##0");
            costLabel.Text = page.ConvertToCurrencyString(0m);
        }
    }

}
